<template>
  <span>
    <RadioGroup v-model="inputData">
      <Radio v-for="(row,index) in data"
             :disabled="disabled"
             :key="index"
             :name="name"
             :label="radioValue(row,index)">{{radioText(row,index)}}</Radio>
      <Radio v-if="hasAll"
             :disabled="disabled"
             :name="name"
             :label="null">全部</Radio>
    </RadioGroup>
  </span>
</template>
<script>
import { isArray } from '@/common';
isArray;
export default {
	props: {
		disabled: {},
		hasAll: {},
		data: {
			default() {
				return [];
			},
		},
		name: {},
		nameKey: {
			default: 'name',
		},
		valueKey: {
			default: 'id',
		},
		string: {
			default: false,
		},
		value: {},
	},
	data() {
		return {
			inputData: null,
		};
	},
	watch: {
		inputData(v) {
			this.$emit('input', v);
		},
		value(v) {
			if (typeof v === 'number') {
				v = v.toString();
			}
			this.inputData = v;
		},
	},
	methods: {
		radioText(row, index) {
			if (typeof row === 'object') {
				return row[this.nameKey];
			} else {
				return row;
			}
		},
		radioValue(row, index) {
			if (this.string) {
				return row;
			} else {
				if (typeof row === 'object') {
					return row[this.valueKey];
				} else {
					return index;
				}
			}
		},
		radioChange(v) {
			this.$emit('input', v);
		},
	},
	mounted() {
		this.inputData = this.value;
		if (typeof this.value === 'number') {
			this.inputData = this.inputData.toString();
		}
	},
};
</script>


